
/*
        HTMLget written by Christopher E. Miller
        This code is public domain.
        You may use it for any purpose.
        This code has no warranties and is provided 'as-is'.
 */

 debug = HTMLGET;

import std.string, std.conv, std.stream;
import std.socket, std.socketstream;
import std.stdio;

import std.json;
auto val = &parseJSON!string; // workaround for bug in compiler

static import wizzAirUrls;


void main()
{
	//writeln("urls ", wizzAirUrls.urls);
	wizzAirUrls.precacheUrls("/tmp/urlcache");
	wizzAirUrls.findTimetable("/tmp/urlcache");
}	
	
void socketUrlGet(){	
	
	ushort port=8080;
	string domain = "localhost";
	string url = "/download/pdxYWLmC/mysql-554-m3-linux26-x86_64tar.gz?tsid=20110418-110847-b596a886";
	 //  http://localhost:8080/download/pdxYWLmC/mysql-554-m3-linux26-x86_64tar.gz?tsid=20110418-110847-b596a886
	
	writeln(1122);
    Socket sock = new TcpSocket(new InternetAddress("localhost", port)); 
    Stream ss        = new SocketStream(sock);

    debug (HTMLGET)
        writeln("Conn ected!\nRequesting URL \"" ~ url ~ "\"...\n");

    if (port != 80)
        domain = domain ~ ":" ~ to!string(port);

    ss.writeString("GET " ~ url ~ " HTTP/1.1\r\n"
                   "Host: " ~ domain ~ "\r\nConnection: close\r\n"
                   "\r\n");

    // Skip HTTP header.
    char[] line;

    for (;;)
    {
        line = ss.readLine();
        writeln("recv ", line);

        if (!line.length)
            break;

        const char[] CONTENT_TYPE_NAME = "Content-Type: ";

        if (line.length > CONTENT_TYPE_NAME.length &&
            !icmp(CONTENT_TYPE_NAME, line[0 .. CONTENT_TYPE_NAME.length]))
        {
            char[] type;
            type = line[CONTENT_TYPE_NAME.length .. line.length];

            if (type.length <= 5 || icmp("text/", type[0 .. 5]))
                throw new Exception("URL is not text");
        }
    }

print_lines:

    while (!ss.eof())
    {
        line = ss.readLine();
        printf("%.*s\n", line);

        // if(std.string.ifind(line, "</html>") != -1)
        //	break;
        size_t iw;

        for (iw = 0; iw != line.length; iw++)
        {
            if (!icmp("</html>", line[iw .. line.length]))
                break print_lines;
        }
    }


}